package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.AdminDao;
import com.hs.panghu.dao.impl.AdminDaoImpl;
import com.hs.panghu.entity.Admin;
import com.hs.panghu.util.Result;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/login")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.设置编码方式
        // 2.接受请求信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // 3.数据验证
        // 4.链接数据库
        AdminDao adminDao = new AdminDaoImpl();
        Admin admin = adminDao.selectAdminByUsernameAndPassword(username, password);
        // 5.给出响应
        if(admin==null){  // 用户名或密码有误
            Result error = Result.error();
            String srr = JSON.toJSONString(error);
            resp.getWriter().write(srr);
        }else{  // 登录成功
            Result success = Result.success();
            resp.getWriter().write( JSON.toJSONString(success) );
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

